Skip to content
  • 0 Votes
    7 Posts
    375 Views
    L

    well at the end i simply sended all the data and split everything inside a list, not the best thing to do but it works, heres the code

    def servidorMk3(): host = socket.gethostname() host_port = 8000 host_ip = socket.gethostbyname(host) buffer = 1024 code_end = "<END>" code_ok = "<OK>" code_fin = "<FIN>" code_sep = "<SEP>" nombre, size, dataFile, elemt = [], [], [], [] print (f"Servidor Mk3") print (f"Ip del servidor: " + host_ip) log("Inicio del servidorMk3 " + host_ip) with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as tcp_conn: tcp_conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) tcp_conn.bind((host_ip, host_port)) tcp_conn.listen(1) con, adrs = tcp_conn.accept() print (f"Conecion Exitosa") log("Connecion exitosa con el server: " + adrs[0]) while True: alldata = con.recv(buffer) while True: # recibe la info en general alldata = alldata + con.recv(buffer) if code_fin.encode("utf8") in alldata: # si llega <FIN>, se acabo la lista break data = alldata.split(b'<SEP>') #separar manualmente while code_fin.encode("utf8") in data: data.remove(code_fin.encode("utf8")) for i in range(0, len(data),3): bloque = data[i:i+3] nombre.append(bloque[0].decode("utf8")) print(nombre) size.append(bloque[1].decode("utf8")) dataFile.append(bloque[2]) directorio = crear_directorio() for x in range(len(nombre)): if directorio == "<BAD>": print(" no se puede crear la ubicacion") print(type(directorio)) # revisar el tipo que regresa esta funcion¡¡¡¡ print(directorio) break else: print(f"0 .. {nombre[x]}") print(f"1 .. {size[x]}") #print (dataFile) file_name = directorio + nombre[x] # path to the file, file_size = int(size[x]) # size of file file_create = open(file_name, 'wb') # open file for writing in binary file_create.write(dataFile[x]) # writes to the file file_create.close() #close the file check = os.path.getsize(file_name) # returns the size of the file print (f"Tamaño esperado: {file_size} ... tamaño real {check} ") if check == file_size: log("archivo " + file_name + " creado, tamaño: " + str(file_size)) else: log("No se pudo crear el archivo: " + file_name) if x == len(nombre): log("Respaldo de Archivos completado") tcp_conn.close() print(f"Respaldo de archivos terminada")